﻿// MIT License (MIT)
// 
// Copyright (c) 2014 K Ercan Turkarslan
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 

using System;
using Mios.Generated.ServiceFiles;

namespace Mios
{
    public class MiosDevice : MiosItem
    {
        #region Luup Request
        // Device related Http Luup Request methods
        // http://wiki.micasaverde.com/index.php/Luup_Requests
        //
        public String Luup_Actions(String outputFormat = null)
        {
            return Engine.Luup_Actions(Id, outputFormat);
        }

        public String Luup_Status(String outputFormat = null)
        {
            return Engine.Luup_Status(Id, outputFormat);
        }

        public String Luup_DeviceRename(String newName, UInt32 newRoom)
        {
            return Engine.Luup_DeviceRename(Id, newName, newRoom);
        }

        public String Luup_DeviceRename(String newName, String newRoom = null)
        {
            return Engine.Luup_DeviceRename(Id, newName, newRoom);
        }

        public String Luup_DeviceDelete()
        {
            return Engine.Luup_DeviceDelete(Id);
        }

        public String Luup_CallAction(String serviceId, String action, params String[] parameters)
        {
            return Engine.Luup_CallAction(Id, serviceId, action, parameters);
        }

        public String Luup_VariableSet(String serviceId, String varName, String value)
        {
            return Engine.Luup_VariableSet(Id, serviceId, varName, value);
        }

        public String Luup_VariableGet(String serviceId, String varName)
        {
            return Engine.Luup_VariableGet(Id, serviceId, varName);
        }

        public String Luup_FindDeviceByAltId(UInt32 altid)
        {
            return Engine.Luup_FindDeviceByAltId(altid);
        }

        public String Luup_FindDevice()
        {
            return Engine.Luup_FindDevice(Id);
        }

        public String Luup_Invoke()
        {
            return Engine.Luup_Invoke(Id);
        }

        #endregion

        public UInt32 DeviceId { get { return base.Id; } }

        public String Name { get; private set; }


        public MiosRoom Room { get; private set; }

        // ReSharper disable once InconsistentNaming
        public ZWaveDevice1Service SZWaveDevice1 { get; private set; }


        #region SetVariable Method
        internal virtual Boolean SetVariable(String serviceId, String variable, String value)
        {
            return false;
        }
        #endregion SetVariable Method

        #region SetShortCode Method
        internal virtual Boolean SetShortCode(String shortCode, String value)
        {
            return false;
        }
        #endregion SetVariable Method

        #region Constructor(s)
        public MiosDevice(MiosEngine engine, UInt32 id, String name = null, MiosRoom room = null)
            : base(engine, id)
        {
            SZWaveDevice1 = new ZWaveDevice1Service(engine, id, "urn:micasaverde-com:serviceId:ZWaveDevice1");

            Name = name;
            Room = room;

            if (room != null) 
                room.AddDevice(this);
        }

#if USE_SDATA || USE_USERDATA
        public String LocalUdn { get; private set; }
        public String DeviceType { get; private set; }

        internal MiosDevice(MiosEngine engine, UInt32 id, String name, String localUdn, String deviceType, MiosRoom room)
            : base(engine, id)
        {
            if (name == null) 
                throw new ArgumentNullException("name");

            if (localUdn == null)
                localUdn = "";

            if (deviceType == null) 
                throw new ArgumentNullException("deviceType");

            if (room == null) 
                throw new ArgumentNullException("room");

            SZWaveDevice1 = new ZWaveDevice1Service(engine, id, "urn:micasaverde-com:serviceId:ZWaveDevice1");

            Name = name;
            LocalUdn = localUdn;
            DeviceType = deviceType;
            Room = room;

            room.AddDevice(this);
        }
#endif
        #endregion
    }
}
